package com.hang.springrabbitmq.ttlQueue.controller;

import com.hang.springrabbitmq.ttlQueue.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

//发送延迟消息
@Slf4j
@Controller
@RequestMapping("/wwtt")
public class SendMsgController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //发消息
    @GetMapping("/senMsg/{message}")
    public void senMsg(@PathVariable String message){
        log.info("当前时间:{},发送一条信息给两个ttl队列:{}",new Date().toString(),message);

        rabbitTemplate.convertAndSend("X","XA","消息来自wwtt为10s的队列：" + message);
        rabbitTemplate.convertAndSend("X","XB","消息来自wwtt为40s的队列：" + message);

    }

    @GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
    public void sendMsg(@PathVariable("message") String message,@PathVariable("ttlTime")String ttlTime){
        log.info("当前时间:{},发送一条时长{}毫秒TTL信息队列QC:{}",new Date().toString(),ttlTime,message);
        rabbitTemplate.convertAndSend("X","XC",message,msg->{
            msg.getMessageProperties().setExpiration(ttlTime);
            return msg;
        });
    }

    //基于插件发送的延迟时间的队列
    @GetMapping("/sendDelayMsg/{message}/{delayTime}")
    public void sendMsg(@PathVariable String message,@PathVariable Integer delayTime){
        log.info("当前时间：{}，发送一条时长{}毫秒信息给延迟队列delayed.queue：{}",new Date(),delayTime,message);
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME, DelayedQueueConfig.DELAYED_ROUTING_KEY,message, msg ->{
            //发送消息的时候 延迟时长
            msg.getMessageProperties().setDelay(delayTime);
            return msg;
        });
    }
}
